/**
 * 
 */
package com.android.gtdroid;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Application;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.util.Log;

/**
 * @author xlees
 * 
 */
public class GTDApp extends Application {
	public static final int INBOX = 0;
	public static final int NEXT_ACTION = 1;
	public static final int AGENDA = 2;
	public static final int WAITTING = 3;
	public static final int MEMO = 4;
//	public static final int REF = 5;
	
	private SQLiteDatabase db = null;
	private DBOpenHelper dbHelper;

	private DBListAdapter dbla;
	private DBTaskAdapter dbta;
	private Cursor listCursor;
	private Cursor taskCursor;

	private ArrayList<ListItem> listItems;
	public static ArrayList<String> lists;
	private ArrayList<TaskItem> taskItems;
	private ArrayList<String> contexts;
	private ArrayList<String> projects;
	private ArrayList<String> positions;
	private ArrayList<String> tags;
	private ArrayList<String> sort;
	
	public ArrayList<String> repeat;
	public ArrayList<String> remind;
	public ArrayList<String> prior;
	public HashMap<String, Integer> sortType;

	private SharedPreferences spLists;
	private SharedPreferences spContexts;
	private SharedPreferences spProjects;
	private SharedPreferences spPositions;
	private SharedPreferences spTags;
	private SharedPreferences spSort;

	private final String strList = "lists";
	private final String strContext = "ctts";
	private final String strProject = "prjs";
	private final String strPosition = "pos";
	private final String strTag = "tags";
	private final String strSort = "sort";
	private final int MODE = MODE_PRIVATE;

	private final String tag = "GTDApp";
	
	
	public ArrayList<String> getSort() {
		return sort;
	}

	public void open() throws SQLiteException {
		// try {
		db = dbHelper.getWritableDatabase();
		// } catch (SQLiteException ex) {
		// db = dbHelper.getReadableDatabase();
		// }
	}

	/*
	 * @Override protected void finalize() throws Throwable { // TODO
	 * Auto-generated method stub if (dbHelper != null) dbHelper.close(); if (db
	 * != null) db.close();
	 * 
	 * super.finalize(); }
	 */

	public void close() {
		db.close();
	}

	public void updateList() {
		listCursor = dbla.getAllListItems();

		if (listCursor.getCount() == 0) { // initialize lists only when there is
											// no list
			initLists();
			listCursor = dbla.getAllListItems(); // requery cursor
		}

		listItems.clear();
		if (listCursor.moveToFirst()) {
			do {
				String list = listCursor.getString(DBListAdapter.COL_LIST_NAME);
				String note = listCursor.getString(DBListAdapter.COL_LIST_NOTE);
				listItems.add(new ListItem(list, note));
			} while (listCursor.moveToNext());
		}
	}

	public void updateTask() {
		taskCursor = dbta.getAllTaskItems();

		taskItems.clear();
		if (taskCursor.moveToFirst()) {
			do {
				String task = taskCursor.getString(DBTaskAdapter.COL_TASK_NAME);
				String note = taskCursor.getString(DBTaskAdapter.COL_TASK_NOTE);
				String list = taskCursor.getString(DBTaskAdapter.COL_TASK_LIST);

				TaskItem ti = new TaskItem(task, note);
				ti.setListName(list);
				taskItems.add(ti);
			} while (taskCursor.moveToNext());
		}
	}

	public void initLists() {
		dbla.insertList(new ListItem("收集篮", ""));
		dbla.insertList(new ListItem("下一步行动", ""));
		dbla.insertList(new ListItem("日程", ""));
		dbla.insertList(new ListItem("等待", ""));
		dbla.insertList(new ListItem("备忘录", ""));
//		dbla.insertList(new ListItem("参考资料", ""));
	}

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();

		Log.i(tag, "onCreate method is called!");

		dbHelper = new DBOpenHelper(this);
		open();

		if (db == null)
			Log.e(tag, "db is null!!");

		dbla = new DBListAdapter(db);
		dbta = new DBTaskAdapter(db);

		listCursor = null;
		taskCursor = null;
		listItems = new ArrayList<ListItem>();
		taskItems = new ArrayList<TaskItem>();
		
		updateList();
		updateTask();
		
		// list
		lists = new ArrayList<String>();
		spLists = getSharedPreferences(strList, MODE);
		if (spLists.contains("exist")) {
			int count = spLists.getInt("count", 0);
			for (int i = 0; i < count; i++) {
				String str = spLists.getString("List["+i+"]", null);
				lists.add(str);
			}
		} else {
			lists.add("收集篮");			// 0
			lists.add("下一步行动");		// 1
			lists.add("日程");			// 2
			lists.add("等待");			// 3
			lists.add("备忘录");			// 4
			
			Editor edit = spLists.edit();
			edit.putBoolean("exist", true);
			edit.putInt("count", lists.size());
			for(int i=0; i<lists.size(); i++){
			     edit.putString("List["+i+"]", lists.get(i));
			}
			edit.commit();
		}

		// context
		contexts = new ArrayList<String>();
		spContexts = getSharedPreferences(strContext, MODE);
		if (spContexts.contains("exist")) { // context file exists
			int count = spContexts.getInt("count", 0);
			for (int i = 0; i < count; i++) {
				String str = spContexts.getString("Context["+i+"]", null);
				contexts.add(str);
			}
		} else { // the first time
			// default contexts, and "no context" can not be deleted
			contexts.add("未指定");
			contexts.add("电话");
			contexts.add("电脑");
			
			Editor edit = spContexts.edit();
			edit.putBoolean("exist", true);
			edit.putInt("count", contexts.size());
			for(int i=0; i<contexts.size(); i++){
			     edit.putString("Context["+i+"]", contexts.get(i));
			}
			edit.commit();
		}
		
		projects = new ArrayList<String>();
		spProjects = getSharedPreferences(strProject, MODE);
		if (spProjects.contains("exist")) { // context file exists
			int count = spProjects.getInt("count", 0);
			for (int i = 0; i < count; i++) {
				String str = spProjects.getString("Project["+i+"]", null);
				projects.add(str);
			}
		} else { // the first time
			// default projects, and "no context" can not be deleted
			projects.add("未指定");
			
			Editor edit = spProjects.edit();
			edit.putBoolean("exist", true);
			edit.putInt("count", projects.size());
			for(int i=0; i<projects.size(); i++){
			     edit.putString("Project["+i+"]", projects.get(i));
			}
			edit.commit();
		}
		
		positions = new ArrayList<String>();
		spPositions = getSharedPreferences(strPosition, MODE);
		if (spPositions.contains("exist")) { // context file exists
			int count = spPositions.getInt("count", 0);
			for (int i = 0; i < count; i++) {
				String str = spPositions.getString("Position["+i+"]", null);
				positions.add(str);
			}
		} else { // the first time
			// default projects, and "no context" can not be deleted
			positions.add("未指定");
			
			Editor edit = spPositions.edit();
			edit.putBoolean("exist", true);
			edit.putInt("count", positions.size());
			for(int i=0; i<positions.size(); i++){
			     edit.putString("Position["+i+"]", positions.get(i));
			}
			edit.commit();
		}
		
		// tags
		tags = new ArrayList<String>();
		spTags = getSharedPreferences(strTag, MODE);
		if (spTags.contains("exist")) { // context file exists
			int count = spTags.getInt("count", 0);
			for (int i = 0; i < count; i++) {
				String str = spTags.getString("Tag["+i+"]", null);
				tags.add(str);
			}
		} else { // the first time
//			tags.add("无标签");
			
			Editor edit = spTags.edit();
			edit.putBoolean("exist", true);
			edit.putInt("count", tags.size());
			for(int i=0; i<tags.size(); i++){
			     edit.putString("Tag["+i+"]", tags.get(i));
			}
			edit.commit();
		}
		
		// repeat
		repeat = new ArrayList<String>();
		repeat.add(0,"不重复");
		repeat.add(1,"按天");
		repeat.add(2,"按周");
		repeat.add(3,"按工作日");
		repeat.add(4,"按月");
		repeat.add(5,"按年");
		repeat.add(6,"直到");
		
		//remind
		remind = new ArrayList<String>();
		remind.add(0,"任务开始时");
		remind.add(1,"提前10分钟");
		remind.add(2,"提前30分钟");
		remind.add(3,"提前1个小时");
		remind.add(4,"提前2个小时");
		
		//prior
		prior = new ArrayList<String>();
		prior.add(0,"无");
		prior.add(1,"高");
		prior.add(2,"中");
		prior.add(3,"低");
		
		
		// sort
		sort = new ArrayList<String>();
		sort.add(0,"默认");
		sort.add(1,"按情境");
		sort.add(2,"按位置");
		sort.add(3,"按项目");
		sort.add(4,"按优先级");
		
		// sort type, default is time
		sortType = new HashMap<String, Integer>();
		spSort = getSharedPreferences(strSort, MODE);
		if (spSort.contains("exist")) { // context file exists
			sortType.put("inbox", spSort.getInt("inbox", 0));
			sortType.put("next", spSort.getInt("next", 0));
			sortType.put("agenda", spSort.getInt("agenda", 0));
			sortType.put("waiting", spSort.getInt("waiting", 0));
			sortType.put("memo", spSort.getInt("memo", 0));
			sortType.put("completed", spSort.getInt("completed", 0));
			sortType.put("trash", spSort.getInt("trash", 0));
		} else { // the first time
			Editor edit = spSort.edit();
			edit.putBoolean("exist", true);
			
			edit.putInt("inbox", 0);
			edit.putInt("next", 0);
			edit.putInt("agenda", 0);
			edit.putInt("waiting", 0);
			edit.putInt("memo", 0);
			edit.putInt("completed", 0);
			edit.putInt("trash", 0);
			
			edit.commit();
		}
	}
	
	public SharedPreferences getSpSortType() {
		return spSort;
	}

	public SharedPreferences getSpProjects() {
		return spProjects;
	}

	public void setSpProjects(SharedPreferences spProjects) {
		this.spProjects = spProjects;
	}

	public SharedPreferences getSpPositions() {
		return spPositions;
	}

	public void setSpPositions(SharedPreferences spPositions) {
		this.spPositions = spPositions;
	}

	public SharedPreferences getSpTags() {
		return spTags;
	}

	public void setSpTags(SharedPreferences spTags) {
		this.spTags = spTags;
	}

	public SharedPreferences getSpContexts() {
		return spContexts;
	}

	public void setSpContexts(SharedPreferences spContexts) {
		this.spContexts = spContexts;
	}

	@Override
	public void onTerminate() {
		// TODO Auto-generated method stub
		Log.i(tag, "onTerminate method is called!");

		// if (db != null)
		// close();
		super.onTerminate();
	}

	public SQLiteDatabase getDb() {
		return db;
	}

	public void setDb(SQLiteDatabase db) {
		this.db = db;
	}

	public DBOpenHelper getDbHelper() {
		return dbHelper;
	}

	public void setDbHelper(DBOpenHelper dbHelper) {
		this.dbHelper = dbHelper;
	}

	public DBListAdapter getDbla() {
		return dbla;
	}

	public void setDbla(DBListAdapter dbla) {
		this.dbla = dbla;
	}

	public DBTaskAdapter getDbta() {
		return dbta;
	}

	public void setDbta(DBTaskAdapter dbta) {
		this.dbta = dbta;
	}

	public Cursor getListCursor() {
		return listCursor;
	}

	public void setListCursor(Cursor listCursor) {
		this.listCursor = listCursor;
	}

	public Cursor getTaskCursor() {
		return taskCursor;
	}

	public void setTaskCursor(Cursor taskCursor) {
		this.taskCursor = taskCursor;
	}

	public ArrayList<ListItem> getListItems() {
		return listItems;
	}

	public void setListItems(ArrayList<ListItem> listItems) {
		this.listItems = listItems;
	}

	public ArrayList<TaskItem> getTaskItems() {
		return taskItems;
	}

	public void setTaskItems(ArrayList<TaskItem> taskItems) {
		this.taskItems = taskItems;
	}

	public ArrayList<String> getProjects() {
		return projects;
	}

	public void setProjects(ArrayList<String> projects) {
		this.projects = projects;
	}

	public ArrayList<String> getContexts() {
		return contexts;
	}

	public void setContexts(ArrayList<String> contexts) {
		this.contexts = contexts;
	}

	public ArrayList<String> getPositions() {
		return positions;
	}

	public void setPositions(ArrayList<String> positions) {
		this.positions = positions;
	}

	public ArrayList<String> getTags() {
		return tags;
	}

	public void setTags(ArrayList<String> tags) {
		this.tags = tags;
	}

}
